package models.presentation
{
	import events.MidiaEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	import org.swizframework.Swiz;

	public class MidiaViewPresentationModel extends EventDispatcher implements IMidiaViewPresentationModel
	{
		// dispatcher that will be set from constructor (via Prototype tag in Beans.mxml),
		// allowing this non-view class to dispatch bubbling events
		private var dispatcher:IEventDispatcher;
				
		public function MidiaViewPresentationModel(target:IEventDispatcher)
		{
			this.dispatcher = dispatcher;
		}		
		
		/**
		 * Inject the midias property of ApplicationModel into this field.
		 */
		[Autowire( bean="appModel", property="midias" )]
		[Bindable]
		public var midias:ArrayCollection;
				
				
		// ========================================
		// selectedMidia property (read only)
		// ========================================
		
		/**
		 * @private
		 * 
		 * Backing variable for read-only <code>selectedMidia</code> getter.
		 */
		private var _selectedMidia:Object;				

		[Bindable( "selectedMidiaChanged" )]
		public function get selectedMidia():Object
		{
			return _selectedMidia;
		}
		
		// setter method
		public function setSelectedMidia(value:Object):void
		{
			if ( _selectedMidia != value )
			{
				_selectedMidia = value;
				dispatchEvent( new Event( "selectedMidiaChanged" ) );
			}
		}
		
		/**
		 * Views using this model could obviously check selectedMidia != null themselves.
		 * However, one of the main points of using presentation model is for your views
		 * to know as little as possible about the underlying model, and creating an explicit
		 * property is good encapsulation.
		 */
		[Bindable( "selectedMidiaChanged" )]		
		public function get isFormVisible():Boolean
		{
			return selectedMidia != null;
		}
		
		/**
		 * When the view requests an object be saved we simply attach it to an event and dispatch
		 * through our injected dispatcher. This will allow the event to bubble and be caught by Swiz.
		 */		
		public function salvarMidia(midia:Object):void
		{
			//dispatcher.dispatchEvent( new MidiaEvent( MidiaEvent.SALVAR_MIDIA, midia ) );
			Swiz.dispatchEvent( new MidiaEvent( MidiaEvent.SALVAR_MIDIA, midia ) );
		}
		
		public function listarMidias():void
		{
			Swiz.dispatchEvent( new MidiaEvent(MidiaEvent.LISTAR_MIDIAS, null ));
		}
		
		
	}
}